/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BackupFrame.java
 *
 * Created on 12/10/2008, 12:59:35 PM
 */
package org.general.backup;

import org.mybeans.Sesion;
import java.io.File;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;

/**
 *
 * @author avbravo
 */
public class RestaurarBackupFrame extends javax.swing.JFrame {

    Sesion sesion;
    Backup backup;
    String osName;
    String restauracion;

    /** Creates new form BackupFrame */
    public RestaurarBackupFrame() {
        initComponents();
        try {
            sesion = new Sesion();
            backup = new Backup();
            /*
             * Fondo
             */

             
             
             

            jXPanel1.setOpaque(false);
            setContentPane(jXPanel1);
            /*
             * fondo
             */
            backup.GenerarNombreArchivo();
            jFormattedTextFieldFecha.setValue(backup.getMydate());
            jTextFieldArchivo.setText(backup.getArchivo());
            jTextFieldUbicacion.setText(backup.getRuta());

            osName = System.getProperty("os.name");
            osName = osName.toLowerCase();
            File dir;
            if (osName.equals("linux")) {

                dir = new File("/home/avbravo/ingresos/backup");
            } else {
                dir = new File("c:\\ingresos\\backup");
            }
            jComboBoxRespaldos.removeAllItems();
            String[] children = dir.list();
            if (children == null) {
                // Either dir does not exist or is not a directory
            } else {
                int contador = -1;
                for (int i = 0; i < children.length; i++) {
                    // Get filename of file or directory
                    String filename = children[i];
                    jComboBoxRespaldos.insertItemAt(filename, i);
                    contador++;

                }
                if (contador >= 0) {
                    jComboBoxRespaldos.setSelectedIndex(contador);
                    //  jButtonRestaurarBackup.setEnabled(true);
                    jComboBoxRespaldos.setEnabled(true);
                } else {
                    sesion.MostrarMensajeInformacion("Mensaje", "No se ha realizado ningun respaldo de la base de datos");
                }

            }

            AutoCompleteDecorator.decorate(this.jComboBoxRespaldos);
        } catch (Exception e) {
            sesion.MostrarMensajeError("Error", "BackupFrame.java\njButtonBackupActionPerformed\n" + e.getMessage().toString());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jXPanel1 = new org.jdesktop.swingx.JXPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldArchivo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldUbicacion = new javax.swing.JTextField();
        jButtonRestaurarBackup = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jFormattedTextFieldFecha = new javax.swing.JFormattedTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jComboBoxRespaldos = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jTextFieldArchivoRestaurar = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("RESTAURAR BASE DATOS");

        jLabel1.setText("Archivo");

        jTextFieldArchivo.setEditable(false);

        jLabel2.setText("Ubicacion");

        jTextFieldUbicacion.setEditable(false);

        jButtonRestaurarBackup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/restaurarbackup.jpeg"))); // NOI18N
        jButtonRestaurarBackup.setToolTipText("Realizar Restauracion");
        jButtonRestaurarBackup.setEnabled(false);
        jButtonRestaurarBackup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRestaurarBackupActionPerformed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/regresar.jpeg"))); // NOI18N
        jButton1.setToolTipText("Regresar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jFormattedTextFieldFecha.setEditable(false);
        jFormattedTextFieldFecha.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/MM/yyyy"))));
        jFormattedTextFieldFecha.setToolTipText("Fecha");
        jFormattedTextFieldFecha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jFormattedTextFieldFechaActionPerformed(evt);
            }
        });

        jLabel3.setText("Fecha");

        jLabel4.setText("Antres de restaurar la base de datos se realizara un backup de la misma.");

        jLabel5.setText("Respaldos Realizados");

        jComboBoxRespaldos.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBoxRespaldos.setEnabled(false);
        jComboBoxRespaldos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxRespaldosActionPerformed(evt);
            }
        });

        jLabel6.setText("Se restaura este:");

        jTextFieldArchivoRestaurar.setEnabled(false);

        jLabel7.setText("Formato: año mes dia hora minutos");

        javax.swing.GroupLayout jXPanel1Layout = new javax.swing.GroupLayout(jXPanel1);
        jXPanel1.setLayout(jXPanel1Layout);
        jXPanel1Layout.setHorizontalGroup(
            jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addComponent(jLabel4)
                .addContainerGap())
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jXPanel1Layout.createSequentialGroup()
                        .addComponent(jButtonRestaurarBackup, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(27, 27, 27)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(317, 317, 317))
                    .addGroup(jXPanel1Layout.createSequentialGroup()
                        .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldArchivoRestaurar, javax.swing.GroupLayout.DEFAULT_SIZE, 649, Short.MAX_VALUE)
                            .addComponent(jLabel7)
                            .addComponent(jComboBoxRespaldos, javax.swing.GroupLayout.PREFERRED_SIZE, 389, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())))
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextFieldArchivo, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jFormattedTextFieldFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldUbicacion, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(147, 147, 147))
        );
        jXPanel1Layout.setVerticalGroup(
            jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jXPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel1))
                    .addGroup(jXPanel1Layout.createSequentialGroup()
                        .addComponent(jFormattedTextFieldFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jTextFieldArchivo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldUbicacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel4)
                .addGap(3, 3, 3)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jComboBoxRespaldos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTextFieldArchivoRestaurar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButtonRestaurarBackup, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jXPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jXPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButtonRestaurarBackupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRestaurarBackupActionPerformed
        // TODO add your handling code here:
        try {

            backup.EjecutarBackup();
            backup.EjecutarRespaldo(restauracion);

        } catch (Exception e) {
            sesion.MostrarMensajeError("Error", "BackupFrame.java\njButtonBackupActionPerformed\n" + e.getMessage().toString());
        }
}//GEN-LAST:event_jButtonRestaurarBackupActionPerformed

    private void jFormattedTextFieldFechaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jFormattedTextFieldFechaActionPerformed
}//GEN-LAST:event_jFormattedTextFieldFechaActionPerformed

    private void jComboBoxRespaldosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxRespaldosActionPerformed
        // TODO add your handling code here:
        try {
            restauracion = (String) jComboBoxRespaldos.getSelectedItem();
            jTextFieldArchivoRestaurar.setText(restauracion);
            if (restauracion == null || restauracion.equals("")) {
                jButtonRestaurarBackup.setEnabled(false);
            } else {
                jButtonRestaurarBackup.setEnabled(true);
            }
            
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Clase:RestaurarFrame.java\nMetodo:jComboBoxRespaldosActionPerformed()\nError: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jComboBoxRespaldosActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new RestaurarBackupFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButtonRestaurarBackup;
    private javax.swing.JComboBox jComboBoxRespaldos;
    private javax.swing.JFormattedTextField jFormattedTextFieldFecha;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField jTextFieldArchivo;
    private javax.swing.JTextField jTextFieldArchivoRestaurar;
    private javax.swing.JTextField jTextFieldUbicacion;
    private org.jdesktop.swingx.JXPanel jXPanel1;
    // End of variables declaration//GEN-END:variables
}
